﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Xml.XPath;

namespace Templay
{
    /// <summary>
    /// Parser xml for reading the 
    /// </summary>
    public class XmlParser : IParser
    {

        public ParseElementCollection Parse(string template)
        {
            ParseElementCollection list = new ParseElementCollection();

            XDocument document = XDocument.Parse(template, LoadOptions.None); // Create xml document

            foreach (XElement element in document.Elements())
                list.Add(CreateParseElement(element));

            return list;
        }

        private ParseElement CreateParseElement(XElement element)
        {
            ParseElement parseElement = new ParseElement(element.Name.LocalName);

            foreach (XAttribute attribute in element.Attributes())
                parseElement.Attributes.Add(attribute.Name.LocalName, new ParseAttribute() { Name = attribute.Name.LocalName, Value = attribute.Value });

            foreach (XElement childElement in element.Elements())
                parseElement.Elements.Add(CreateParseElement(childElement));

            return parseElement;
        }

    }

}
